################################################################################
##
## Purpose: This script creates Figure 11, along with SI 9 and 10.
##
## Author: James Bisbee (james.h.bisbee@vanderbilt.edu)
##
## Input Files:
##  - ./data/prepped/finalData.RData: Prepped data from 9_DATA_final_build.R
##
## Output Files:
##  - ./output/figures/MS_figure_11.pdf
##  - ./output/figures/SI_table_9.tex
##  - ./output/figures/SI_table_10.tex
##
##
## See associated log file for compute environment, package versions, 
##  and date of most recent run.
##
################################################################################
rm(list = ls())
gc()
require(tidyverse)
require(ggridges)
require(fixest)

set.seed(123)

# Compute details
print(paste0('Compute environment from ',Sys.Date(),' run by Bisbee'))
if(Sys.info()['sysname'] == 'Windows') {
  ram_size = system("wmic MemoryChip get Capacity", intern = TRUE)[-1]
  model_name = system("wmic cpu get name", intern = TRUE)[2] # nocov
  vendor_id = system("wmic cpu get manufacturer", intern = TRUE)[2] # nocov
  
  print(list(ram = stringr::str_squish(ram_size)[1],
             vendor_id = stringr::str_squish(vendor_id),
             model_name = stringr::str_squish(model_name),
             no_of_cores = parallel::detectCores()))
} else if(Sys.info()['sysname'] == 'Linuxs') {
  splitted <- strsplit(system("ps -C rsession -o %cpu,%mem,pid,cmd", intern = TRUE), " ")
  df <- do.call(rbind, lapply(splitted[-1], 
                              function(x) data.frame(
                                cpu = as.numeric(x[2]),
                                mem = as.numeric(x[4]),
                                pid = as.numeric(x[5]),
                                cmd = paste(x[-c(1:5)], collapse = " "))))
  df
} else {
  cat("If not on Linux or Windows, you'll have to figure out your own solution to seeing the compute environment.")
}

sessionInfo()


load('./data/prepped/finalData.RData')


# Is Yellen more toxic?
dyadToAnalYellen <- utterance_level %>%
  arrange(fullInd) %>%
  mutate_at(vars(matches('lag')),function(x) ifelse(is.na(x),0,x)) %>%
  mutate(votepct_rel = ifelse(is.infinite(votepct_rel),1,votepct_rel),
         yellen = ifelse(grepl('YELLEN',opensecretsID),1,0),
         respondingTo = relevel(factor(respondingTo),ref = 'FEDBERNANKE'),
         opensecretsID = relevel(factor(opensecretsID),ref = 'FEDYELLEN'),
         respGroup = ifelse(grepl('YELLEN',respondingTo),'Yellen',
                            ifelse(grepl('BERNANKE',respondingTo),'Bernanke',
                                   ifelse(grepl('GREENSPAN',respondingTo),'Greenspan',
                                          ifelse(grepl('POWELL',respondingTo),'Powell',lag(party)))))) %>% 
  filter(ind > mind,all > 30)



# Are legislators more toxic toward Yellen?
dyadToAnalLegs <- utterance_level %>%
  arrange(fullInd) %>%
  mutate_at(vars(matches('lag')),function(x) ifelse(is.na(x),0,x)) %>%
  mutate(votepct_rel = ifelse(is.infinite(votepct_rel),1,votepct_rel),
         yellen = ifelse(grepl('YELLEN',respondingTo),1,0),
         respondingTo = relevel(factor(respondingTo),ref = 'FEDYELLEN'),
         opensecretsID = relevel(factor(opensecretsID),ref = 'FEDYELLEN'),
         respGroup = relevel(factor(ifelse(grepl('YELLEN',respondingTo),'Yellen',
                                           ifelse(grepl('BERNANKE',respondingTo),'Bernanke',
                                                  ifelse(grepl('GREENSPAN',respondingTo),'Greenspan',
                                                         ifelse(grepl('POWELL',respondingTo),'Powell',lag(party)))))),ref = 'Yellen')) %>% 
  filter(ind > mind,all > 30)

dims <- colnames(utterance_level %>% select(matches('SENT_'),-matches('_lag|error')) %>% select(-matches('SEVERE|AUTHOR|LIKELY')))

# Is yellen more toxic, attacking, or incoherent compared to other fed chairs?
toplot <- NULL
mList <- mListRev <- list()
for(y in dims) {
  (mList[[y]] <- feols(as.formula(paste0('scale(',y,') ~ yellen + ',
                                         paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                         ' + ',
                                         paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                         ' + poly(log(nchars_lag+1),3) + scale(log(tot_utterances)) + interrupted',
                                         '| respondingTo + chamber')),
                       dyadToAnalYellen %>%
                         filter(grepl('FED',opensecretsID),
                                !grepl('FED',respondingTo)),cluster = c('respondingTo','docID')))
  
  toplot <- bind_rows(toplot,mList[[y]]$coeftable %>%
                        data.frame() %>%
                        rename(est = Estimate,se = Std..Error,tstat = t.value,pval = Pr...t..) %>%
                        slice(1) %>%
                        mutate(out = y,
                               model = 'fedTone') %>% as_tibble())
  
  (mListRev[[y]] <- feols(as.formula(paste0('scale(',y,') ~ yellen + ',
                                            paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                            ' + ',
                                            paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                            ' + poly(log(nchars_lag+1),3) + scale(log(tot_utterances)) + interrupted',
                                            '| opensecretsID + chamber')),
                          dyadToAnalLegs %>%
                            filter(grepl('FED',respondingTo),
                                   !grepl('FED',opensecretsID)),cluster = c('respondingTo','docID')))
  
  toplot <- bind_rows(toplot,mListRev[[y]]$coeftable %>%
                        data.frame() %>%
                        rename(est = Estimate,se = Std..Error,tstat = t.value,pval = Pr...t..) %>%
                        slice(1) %>%
                        mutate(out = y,
                               model = 'legTone') %>% as_tibble())
}

toplot2 <- toplot %>%
  mutate(sentType = ifelse(out %in% paste0('SENT_',c('ATTACK_ON_AUTHOR','ATTACK_ON_COMMENTER','IDENTITY_ATTACK','INSULT','THREAT','combAttack')),'Aggression',
                           ifelse(out %in% paste0('SENT_',c('TOXICITY','SEVERE_TOXICITY','PROFANITY','SEXUALLY_EXPLICIT','FLIRTATION','OBSCENE','INFLAMMATORY','combToxic')),'Toxicity',
                                  ifelse(out %in% paste0('SENT_',c('INCOHERENT','UNSUBSTANTIAL','combIncoh')),'Incoherence',NA))),
         out = gsub('Attack On Commenter','Attack',
                    gsub('Combattack','INDEX: Aggression',
                         gsub('Combincoh','INDEX: Incoherence',
                              gsub('Combtoxic','INDEX: Toxicity',str_to_title(gsub('_',' ',gsub('SENT_','',out)))))))) %>%
  mutate(index = ifelse(grepl('INDEX',out),'index','dimension')) %>%
  mutate(model = ifelse(model == 'fedTone',"Tone of Fed Chair","Tone of Responses to Fed Chair")) %>%
  filter(!is.na(sentType),
         !out %in% c('Attack On Author','Severe Toxicity'),
         !grepl('INDEX:',out))

pdf('./output/figures/MS_figure_11.pdf',width = 8,height = 7)
toplot2 %>%
  mutate(out = factor(out,
                      levels = toplot2 %>%
                        filter(model == 'Tone of Responses to Fed Chair') %>%
                        arrange(est) %>% pull(out))) %>%
  ggplot(aes(x = est,y = out,shape = index,fill = index,size = index)) + 
  geom_errorbarh(aes(xmin = est - 2*se,xmax = est + 2*se),height = .1,size = .5) + 
  geom_point() + 
  geom_vline(xintercept = 0,linetype = 'dashed') + 
  theme_ridges() + 
  scale_shape_manual(values = c(19,21)) + 
  scale_size_manual(values = c(2,4)) + 
  scale_fill_manual(values = c('white','white')) + 
  facet_grid(sentType~model,scales = 'free_y',shrink = TRUE,space = 'free_y') + 
  theme(legend.position = 'none') + 
  xlab("Yellen relative to male Fed chairs") + 
  ylab('Tone Dimension')
dev.off()

# Tables
dict <- mList$SENT_TOXICITY$coeftable %>%
  data.frame() %>%
  mutate(covs = row.names(.)) %>%
  filter(grepl('yellen|year|interrupted|SENT|nchars',covs)) %>%
  pull(covs)

dict <- gsub('_lag\\)','_lag',gsub('scale\\(','',dict))

names(dict) <- mList$SENT_TOXICITY$coeftable %>%
  data.frame() %>%
  mutate(covs = row.names(.)) %>%
  filter(grepl('yellen|year|interrupted|SENT|nchars',covs)) %>%
  pull(covs)

dict <- trimws(gsub('  \\+ 1|, 3','',gsub('ITY|ENTITY|UALLY|ICIT|ATION|ENTER|MATORY|STANTIAL|TAC| ON|ERENT','',gsub('_lag',' ',gsub('SENT_','',dict)))))

etable(mList[-which(grepl('comb',names(mList)))],
       headers = gsub('ITY|ENTITY|UALLY|ICIT|ATION|ENTER|MATORY|STANTIAL|TAC| ON|ERENT','',gsub('_',' ',gsub('SENT_','',names(mList[-which(grepl('comb',names(mList)))])))),
       keep = 'yellen|interrupted|TOXY|ID ATK|INSULT|PROFAN|SEX EXPL|THREAT|FLIRT|ATK|INCOH|INFLAM|OBSCENE|UNSUB|nchars',
       depvar = F,
       dict = dict,drop.section = 'fixef',
       extralines = list('100 Topics'=rep('Y',12),
                         'Chamber FE' = rep('Y',12),
                         'Speaker FE' = rep('Y',12)),
       signif.code = c('***' = .001,'**' = .01,'*' = .05,'$\\dag$' = .1),
       replace = T,
       file = './output/tables/SI_table_9.tex')



etable(mListRev[-which(grepl('comb',names(mListRev)))],
       headers = gsub('ITY|ENTITY|UALLY|ICIT|ATION|ENTER|MATORY|STANTIAL|TAC| ON|ERENT','',gsub('_',' ',gsub('SENT_','',names(mListRev[-which(grepl('comb',names(mListRev)))])))),
       keep = 'yellen|interrupted|TOXY|ID ATK|INSULT|PROFAN|SEX EXPL|THREAT|FLIRT|ATK|INCOH|INFLAM|OBSCENE|UNSUB|nchars',
       depvar = F,
       dict = dict,drop.section = 'fixef',
       extralines = list('100 Topics'=rep('Y',12),
                         'Chamber FE' = rep('Y',12),
                         'Speaker FE' = rep('Y',12)),
       signif.code = c('***' = .001,'**' = .01,'*' = .05,'$\\dag$' = .1),
       replace = T,
       file = './output/tables/SI_table_10.tex')

# EOF